package com.test.down.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.text.DecimalFormat;

public class FileUtils
{
    private static final String DOWNLOAD_DIR = "download";

    private static final DecimalFormat DF = new DecimalFormat("0.00");

    public static String getDownloadPerSize(long finished, long total)
    {
        return DF.format((float) finished / (1024 * 1024)) + "M/" + DF.format((float) total / (1024 * 1024)) + "M";
    }

    public static String getDownloadPerSize(long length)
    {
        return DF.format((float) length / (1024 * 1024)) + "M";
    }

    public static final File getDefaultDownloadDir(Context context)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            return new File(context.getExternalCacheDir(), DOWNLOAD_DIR);
        }
        return new File(context.getCacheDir(), DOWNLOAD_DIR);
    }

    public static boolean isSDMounted()
    {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static final String getPrefix(String fileName)
    {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    public static final String getSuffix(String fileName)
    {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
}

